package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import top.fengyulou.xiaofeng.common.MyUtils;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.domain.*;
import top.fengyulou.xiaofeng.dto.PaperDto;
import top.fengyulou.xiaofeng.dto.QuestionDto;
import top.fengyulou.xiaofeng.dto.QuestionOptionDto;
import top.fengyulou.xiaofeng.dto.UserDto;
import top.fengyulou.xiaofeng.enums.StatusEnum;
import top.fengyulou.xiaofeng.mapper.*;
import top.fengyulou.xiaofeng.mapper.my.MyPaperMapper;
import top.fengyulou.xiaofeng.mapper.my.MyQuestionMapper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PaperService
 * @Description TODO
 * @Author qwer
 * @Date 2021/8/22 1:54
 * @Version 1.0
 */
@Service
public class PaperService {

    @Resource
    private PaperMapper paperMapper;
    @Resource
    private MyPaperMapper myPaperMapper;

    /**
     * 列表查询
     */
    public void list(PaperDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<PaperDto> paperList = paperMapper.list(query);
        PageInfo<PaperDto> pageInfo = new PageInfo<>(paperList);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(paperList);
    }

    /**
     * id查询
     */
    public PaperDto getById(String id) {
        return paperMapper.getById(id);
    }

    //保存
    public void save(Paper paper) {
        if (StringUtils.isNotBlank(paper.getId())) {
            //修改
            paper.setUpdateTime(new Date());
            paper.setLastUpdateUserId(paper.getCurrentUserId());
            paperMapper.update(paper);
        } else {
            //添加
            paper.setId(UuidUtil.getShortUuid());
            paper.setCreateTime(new Date());
            paper.setCreateUserId(paper.getCurrentUserId());
            paperMapper.insert(paper);
        }
    }

    //删除
    public void delete(String[] ids) {
        paperMapper.delete(ids);
    }

    /**
     * 查询用户列表
     *
     * @param userDto
     */
    public List<UserDto> findUserList(UserDto userDto) {
        return myPaperMapper.findUserListByPaperId(userDto);
    }

    public List<QuestionDto> getQstListByPaperId(String paperId) {
        return paperMapper.getQstListByPaperId(paperId);
    }

    public void updateRuleQstNum(PaperDto paperDto) {
        paperDto.setUpdateTime(new Date());
        paperDto.setLastUpdateUserId(paperDto.getCurrentUserId());
        paperMapper.updateRuleQstNum(paperDto);
    }

    public void updateRuleQstScore(PaperDto paperDto) {
        paperDto.setUpdateTime(new Date());
        paperDto.setLastUpdateUserId(paperDto.getCurrentUserId());
        paperMapper.updateRuleQstScore(paperDto);
    }

    public void updateQstScore(PaperDto paperDto) {
        paperDto.setUpdateTime(new Date());
        paperDto.setLastUpdateUserId(paperDto.getCurrentUserId());
        paperMapper.updateQstScore(paperDto);
    }

    public void updateQstNum(PaperDto paperDto) {
        paperDto.setUpdateTime(new Date());
        paperDto.setLastUpdateUserId(paperDto.getCurrentUserId());
        paperMapper.updateQstNum(paperDto);
    }
}

